
class Counter{
    private  int count=0;
    public  void func(){
        synchronized (Counter.class){
            count++;

        }
    }
    public void add(){
            count++;
    }
    public int getCount(){
        return count;
    }
}
public class demo20 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter= new Counter();
        Counter counter1=new Counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.func();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                    counter1.func();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count="+counter.getCount());
    }
}
